angular.module("app.sys").register.controller("ListSysRoleCtrl", ["$scope", "$http", "$state", "$modal", "$modalService", function($scope, $http, $state, $modal, $modalService){
  $scope.stores = [];
  $scope.gets = function(){
    $scope.loadTip = '正在获取数据...........';
    $scope.stores = [];
    $http.post('/sys/role/gets/', {}, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $scope.stores = ret.data;
        $scope.stores.length == 0 ? $scope.loadTip = '没有找到相关记录！！' : '';
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.refresh = function(){
    $scope.stores = [];
    $scope.gets();
  };
  //@formatter:off
  $scope.data = {
    entity:{menus:'',status:''},
    opts:{
      statusOpts:[{k:'',t:'请选择'},{k:1,t:'正常'},{k:0,t:'关闭'}],
    }
  };
  //@formatter:on
  $scope.add = function(){
    $http.post('/sys/role/getMenus/', {}, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        var html = '';
        $.each(ret.data.menus, function(k, v){
          html += '<input type="checkbox" data-url="' + v.url + '" data-icon="' + v.icon + '" data-id="' + v.id + '" data-title="' + v.name + '" class="top_menu" value="' + v.id + '"/>' + v.name;
          var addLow = false;
          $.each(v.submenus, function(_k, _v){
            if(!addLow){
              html += '<br/>　└';
              addLow = true;
            }
            html += '&nbsp;<input type="checkbox" data-url="' + _v.url + '" data-icon="' + _v.icon + '" data-id="' + _v.id + '" data-up-id="' + v.id + '" class="sub_menu"  data-title="' + _v.name + '" value="' + _v.id + '"/>' + _v.name + '　';
          });
          html += '<br/>';
        });
        $scope.data.entity.menus = html;
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
    var $modalInstance = $modal.open({
      templateUrl: "/sys/role/add/",
      controller: "AddSysRoleCtrl",
      backdrop: true,
      resolve: {
        data: function(){
          return $scope.data;
        }
      }
    });
    $modalInstance.result.then(function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    }, function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    });
  };
  $scope.edit = function(id){
    $http.post('/sys/role/get/', {
      id: id
    }, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        var html = '';
        $.each(ret.data.menus, function(k, v){
          if(v.checked == 'y'){
            html += '<input type="checkbox" data-url="' + v.url + '" data-icon="' + v.icon + '" data-id="' + v.id + '" data-title="' + v.name + '" class="top_menu" checked="checked" value="' + v.id + '"/>' + v.name;
          }else{
            html += '<input type="checkbox" data-url="' + v.url + '" data-icon="' + v.icon + '" data-id="' + v.id + '" data-title="' + v.name + '" class="top_menu" value="' + v.id + '"/>' + v.name;
          }
          var addLow = false;
          $.each(v.submenus, function(_k, _v){
            if(!addLow){
              html += '<br/>　└';
              addLow = true;
            }
            if(_v.checked == 'y'){
              html += '&nbsp;<input type="checkbox" data-url="' + _v.url + '" data-icon="' + _v.icon + '" data-id="' + _v.id + '" data-up-id="' + v.id + '" class="sub_menu" data-title="' + _v.name + '" checked="checked" value="' + _v.id + '"/>' + _v.name + '　';
            }else{
              html += '&nbsp;<input type="checkbox" data-url="' + _v.url + '" data-icon="' + _v.icon + '" data-id="' + _v.id + '" data-up-id="' + v.id + '" class="sub_menu"  data-title="' + _v.name + '" value="' + _v.id + '"/>' + _v.name + '　';
            }
          });
          html += '<br/>';
        });
        ret.data.menus = html;
        $scope.entity = ret.data;
        var $modalInstance = $modal.open({
          templateUrl: "/sys/role/edit/",
          controller: "EditSysRoleCtrl",
          backdrop: true,
          resolve: {
            data: function(){
              return {
                entity: $scope.entity,
                opts: $scope.data.opts
              };
            }
          }
        });
        $modalInstance.result.then(function(status){
          if(status == 'success'){
            $scope.refresh();
          }
        }, function(status){
          if(status == 'success'){
            $scope.refresh();
          }
        });
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.gets();
}]);
angular.module("app.sys").register.controller("EditSysRoleCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    var menus = {};
    $('.top_menu').each(function(){
      if($(this).prop("checked")){
        var id = $(this).attr('data-id');
        menus['m-k' + id] = {
          id: id,
          title: $(this).attr('data-title'),
          url: $(this).attr('data-url'),
          icon: $(this).attr('data-icon'),
          submenus: {}
        };
      }
    });
    if(menus.length === 0){
      return false;
    }
    $.each(menus, function(k, v){
      $('.sub_menu').each(function(){
        if($(this).attr('data-up-id') == v.id && $(this).prop('checked')){
          var id = $(this).attr('data-id');
          menus['m-k' + v.id]['submenus']['m-k' + id] = {
            id: id,
            title: $(this).attr('data-title'),
            url: $(this).attr('data-url'),
            icon: $(this).attr('data-icon'),
          };
        }
      });
    });
    $scope.entity.id = $scope.entity.role.id;
    $scope.entity.name = $scope.entity.role.name;
    $scope.entity.status = $scope.entity.role.status;
    $scope.entity.menu = JSON.stringify(menus);
    $http.post('/sys/role/edit/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $modalService.myAlert(ret.data);
        $modalInstance.close('success');
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
angular.module("app.sys").register.controller("AddSysRoleCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    var menus = {};
    $('.top_menu').each(function(){
      if($(this).prop("checked")){
        var id = $(this).attr('data-id');
        menus['m-k' + id] = {
          id: id,
          title: $(this).attr('data-title'),
          url: $(this).attr('data-url'),
          icon: $(this).attr('data-icon'),
          submenus: {}
        };
      }
    });
    if(menus.length === 0){
      return false;
    }
    $.each(menus, function(k, v){
      $('.sub_menu').each(function(){
        if($(this).attr('data-up-id') == v.id && $(this).prop('checked')){
          var id = $(this).attr('data-id');
          menus['m-k' + v.id]['submenus']['m-k' + id] = {
            id: id,
            title: $(this).attr('data-title'),
            url: $(this).attr('data-url'),
            icon: $(this).attr('data-icon'),
          };
        }
      });
    });
    $scope.entity.name = $scope.entity.name;
    $scope.entity.status = $scope.entity.status;
    $scope.entity.menu = JSON.stringify(menus);
    $http.post('/sys/role/add/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $modalService.myAlert(ret.data);
        $modalInstance.close('success');
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
